<template>
  <div v-loading="loading">
    <el-form ref="form" :model="form" :rules="rules" label-width="120px" class="dataForm">
      <el-row>
        <!--        <el-col :span="12">-->
        <!--          <el-form-item :label="$t('问卷ID')" prop="questionnaireId">-->
        <!--            <el-input v-model="form.questionnaireId" :placeholder="$t('问卷ID')" show-word-limit maxlength="32" />-->
        <!--          </el-form-item>-->
        <!--        </el-col>-->
        <el-col :span="24">
          <el-form-item :label="$t('题目名称')" prop="name">
            <el-input v-model="form.name" :placeholder="$t('题目名称')" show-word-limit maxlength="300" />
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item :label="$t('是否多选')" prop="isMultipleChoice">
            <el-radio-group v-model="form.isMultipleChoice">
              <el-radio
                v-for="dict in isMultipleChoiceOptions"
                :key="dict.dictValue"
                :label="dict.dictValue"
              >{{ $t(dict.dictLabel) }}
              </el-radio>
            </el-radio-group>
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item :label="$t('题目状态')" prop="status">
            <el-radio-group v-model="form.status">
              <el-radio
                v-for="dict in statusOptions"
                :key="dict.dictValue"
                :label="dict.dictValue"
              >{{ $t(dict.dictLabel) }}
              </el-radio>
            </el-radio-group>
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item :label="$t('排序')" prop="sort">
            <el-input-number v-model="form.sort" controls-position="right" :placeholder="$t('排序')" :precision="0" :max="10000000" />
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item :label="$t('是否启用自由说明')" prop="freedom">
            <el-radio-group v-model="form.freedom">
              <el-radio
                v-for="dict in freedomOptions"
                :key="dict.dictValue"
                :label="dict.dictValue"
              >{{ $t(dict.dictLabel) }}
              </el-radio>
            </el-radio-group>
          </el-form-item>
        </el-col>
      </el-row>
      <div class="area-header">
        <span class="area-header-title">{{ $t('答案列表') }}</span>
      </div>
      <el-table tooltip-effect="darkTable" :data="form.answerKeyList" border class="nowrapTable formTable">
        <el-table-column :label="$t('序号')" align="center" width="50">
          <template slot-scope="scope">
            {{ scope.$index + 1 }}
          </template>
        </el-table-column>
        <el-table-column :label="$t('答案')" align="center">
          <template slot="header" slot-scope="{}">
            <i style="color: #ff4949;margin-right: 4px;">*</i>
            <span>{{ $t('答案') }}</span>
          </template>
          <template slot-scope="scope">
            <el-form-item :prop="'answerKeyList.' + scope.$index + '.answer'" :rules="answerKeyListRules.answer">
              <el-input v-model="scope.row.answer" type="textarea" :autosize="{ maxRows: 5 }" :maxlength="2000" show-word-limit :placeholder="$t('答案')" />
            </el-form-item>
          </template>
        </el-table-column>
        <el-table-column :label="$t('分数')" align="center">
          <template slot="header" slot-scope="{}">
            <i style="color: #ff4949;margin-right: 4px;">*</i>
            <span>{{ $t('分数') }}</span>
          </template>
          <template slot-scope="scope">
            <el-form-item :prop="'answerKeyList.' + scope.$index + '.score'" :rules="answerKeyListRules.score">
              <el-input-number v-model="scope.row.score" controls-position="right" :placeholder="$t('分数')" :precision="1" :max="10000000" />
            </el-form-item>
          </template>
        </el-table-column>
        <el-table-column :label="$t('排序')">
          <template slot="header" slot-scope="{}">
            <i style="color: #ff4949;margin-right: 4px;">*</i>
            <span>{{ $t('排序') }}</span>
          </template>
          <template slot-scope="scope">
            <el-form-item :prop="'answerKeyList.' + scope.$index + '.sort'" :rules="answerKeyListRules.sort">
              <el-input-number v-model="scope.row.sort" controls-position="right" :precision="0" :max="10000000" />
            </el-form-item>
          </template>
        </el-table-column>
        <el-table-column v-if="!otherBtnHide" :label="$t('操作')" align="center" width="60px">
          <template slot-scope="scope">
            <el-button v-hasPermi="['lp:questionnaireSet:remove']" type="text" icon="el-icon-delete" @click="delHandle(scope.$index, scope.row)" />
          </template>
        </el-table-column>
      </el-table>
      <div class="tol">
        <el-button type="primary" icon="el-icon-plus" size="mini" @click="addAnswerKey">{{ $t("添加") }}</el-button>
      </div>
    </el-form>
    <div slot="footer" class="dialog-footer" style="text-align: right;">
      <!--保存 取消按钮-->
      <save-btn has-permi="lp:questionnaireSet" />
    </div>
  </div>
</template>

<script>
    import { saveLpQuestionnaireQuestion, getLpQuestionnaireQuestion, delLpQuestionAnswerKey } from '@/api/fund/questionnaire/settings/questionnaireQuestion/lpQuestionnaireQuestion'
    export default {
        data() {
            return {
                // 是否多选字典
                isMultipleChoiceOptions: [],
                // 题目状态字典
                statusOptions: [],
                // 是否启用自由说明字典
                freedomOptions: [],
                // 表单参数
                form: {
                  answerKeyList: []
                },
                // 遮罩层
                loading: false,
                // 是否隐藏其它按钮
                otherBtnHide: false,
                // 表单校验
                rules: {
                  name: [
                    { required: true, message: this.$t('必填信息'), trigger: 'blur' }
                  ],
                  isMultipleChoice: [
                    { required: true, message: this.$t('必填信息'), trigger: 'blur' }
                  ],
                  status: [
                    { required: true, message: this.$t('必填信息'), trigger: 'blur' }
                  ],
                  sort: [
                    { required: true, message: this.$t('必填信息'), trigger: 'blur' }
                  ],
                  freedom: [
                    { required: true, message: this.$t('必填信息'), trigger: 'blur' }
                  ]
                },
                answerKeyListRules: {
                  answer: [{ required: true, message: this.$t('必填信息'), trigger: 'blur' }],
                  score: [{ required: true, message: this.$t('必填信息'), trigger: 'blur' }],
                  sort: [{ required: true, message: this.$t('必填信息'), trigger: 'blur' }]
                }
            }
        },
        created() {
            this.getDicts('sys_yes_no').then(response => {
                this.isMultipleChoiceOptions = response.data
            })
            this.getDicts('question_status').then(response => {
                this.statusOptions = response.data
            })
            this.getDicts('sys_yes_no').then(response => {
                this.freedomOptions = response.data
            })
        },
        methods: {
            // 初始化数据
            init(data, questionnaireId) {
                this.loading = true
                if (data != null && data.id !== undefined) {
                    getLpQuestionnaireQuestion(data.id).then(response => {
                        this.form = response.data
                        this.form.questionnaireId = questionnaireId
                        this.loading = false
                    }).catch(() => {
                      this.loading = false
                    })
                } else {
                    this.reset()
                    this.form.questionnaireId = questionnaireId
                    this.loading = false
                }
            },
            // 表单重置
            reset() {
                this.form = {
                    id: undefined,
                    questionnaireId: undefined,
                    name: undefined,
                    isMultipleChoice: undefined,
                    status: undefined,
                    sort: undefined,
                    freedom: undefined,
                    remark: undefined,
                    createBy: undefined,
                    createTime: undefined,
                    updateBy: undefined,
                    updateTime: undefined,
                    delFlag: undefined
                }
                this.resetForm('form')
            },
            // 取消按钮
            cancel() {
                this.$parent.$parent.formOpen = false
                this.$parent.$parent.reset()
            },
            // 关闭form页面
            closeForm() {
                // 关闭form页面遮罩层
                this.loading = false
                // 关闭form页面
                if (this.$parent.$parent.formOpen !== undefined) {
                  this.$parent.$parent.formOpen = false
                }
                // 刷新list页面
                if (this.$parent.$parent.getList !== undefined) {
                  this.$parent.$parent.getList()
                }
            },
            /** 提交按钮 */
            submitForm() {
                this.$refs['form'].validate(valid => {
                    if (valid) {
                        this.loading = true
                        saveLpQuestionnaireQuestion(this.form).then(response => {
                            if (response.code === 200) {
                                this.msgSuccess(this.$t('保存成功'))
                                this.closeForm()
                            } else {
                                this.msgError(response.msg)
                            }
                            this.loading = false
                        })
                    } else {
                       this.locationError(document)
                    }
                }).catch(() => {
                    this.loading = false
                })
           },
          // 题目答案
          addAnswerKey() {
            const row = {
              id: undefined,
              questionId: undefined,
              answer: '',
              score: '',
              sort: '',
              remark: ''
            }
            if (!(this.form.answerKeyList instanceof Array)) {
              this.$set(this.form, 'answerKeyList', [])
            }
            this.$nextTick(() => {
              this.form.answerKeyList.push(row)
            })
          },
          // 删除题目答案
          delHandle(index, row) {
            const id = row.id
            if (id !== undefined && id !== '' && id != null) {
              this.$confirm(this.$t('是否确认删除?'), this.$t('警告'), {
                confirmButtonText: this.$t('确定'),
                cancelButtonText: this.$t('取消'),
                type: 'warning'
              })
                .then(function() {
                  return delLpQuestionAnswerKey(id)
                }).then(() => {
                this.form.answerKeyList.splice(index, 1)
                return this.msgSuccess(this.$t('删除成功'))
              })
                .catch(function() {
                })
            } else {
              this.form.answerKeyList.splice(index, 1)
            }
          }
        }
    }
</script>
